package com.twu.biblioteca;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * Created by mauricio on 9/29/14.
 */
public class BookController {

    public void listAction() {
        System.out.println(" [#] -     Title     -   Year   -   Author");
        for (Book book : Book.getAll()) {

            if (book.isAvailable()) {
                System.out.println(book.toString());
            }
        }


    }

    public void checkoutAction() {
        listAction();
        String msg = "That book is not available.";

        System.out.print("Choose a book to checkout: ");
        int input = new Scanner(System.in).nextInt();
        if(Book.isValid(input)) {
            if (Book.getAll().get(input).checkout()) {
                msg = "Thank you! Enjoy the book";
            }
        }

        System.out.println(msg);

    }

    public void devolveAction() {

        ArrayList<Book> checkableToReturn = new ArrayList();

        for (Book checkable: Book.getAll()) {
            if (checkable.checkedBy() == Auth.getLoggedUser()) {
                checkableToReturn.add(checkable);
            }
        }

        String msg = "That is not a valid book to return.";

        if(checkableToReturn.size() > 0 ) {
            System.out.println("Pending to return:\n");

            for (Book checkable : checkableToReturn ) {
                System.out.println(checkable.toString());
            }

            System.out.print("Enter the book id to return: ");

            int input = new Scanner(System.in).nextInt();
            if (Book.isValid(input)) {
                if (Book.getAll().get(input).devolve()) {
                    msg = "Thank you for returning the book.";
                }
            }
        } else {
            msg = "You have anything to return.";
        }

        System.out.println(msg);


    }
}
